﻿using System;
using System.Data;
using System.Windows.Forms;
using HRM.HRMServiceProxy;
using WeifenLuo.WinFormsUI.Docking;
using System.Collections.Generic;
using HRM.BusinessLogic; 

namespace HRM
{
    public partial class frmGradeList : FormBase
    {
        int pageSize = UIParamater.pageSize;
        int currPage = 0;
        int itemCount = 0;
        int totalPage = 0;
        int _GDID = 0;
        //GradeClient _wcfGD;
        RoleRightInfo listRole;
        DataTable dtData = null;


        public override void Show_Form()
        {
            this.Show();
        }


        public frmGradeList()
        {
            InitializeComponent();
        }

        protected override void Init_Control()
        {
            try
            {
                this.KeyPreview = true;
                //_wcfGD = Service.Grade();
                listRole = UIParamater.Quyen_CapDo;
                if (!listRole._create)
                    btnNew.Enabled = false;
                if (!listRole._delete)
                    btnDel.Enabled = false;
                if (!listRole.Update)
                {
                    this.btnEdit.Image = global::HRM.Properties.Resources.app;
                    btnEdit.Text = "xem";
                }
                //

                Init_Grid();
                //
                loadList();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        //Khởi tạo danh sách cho grid
        DataGridHandle m_GridHandle;
        protected void Init_Grid()
        {
            List<DataGridViewColumn> arrColumn = new List<DataGridViewColumn>();

            arrColumn.Add(FormGlobals.Grid_NewColumn<DataGridViewTextBoxColumn>("STT", "RN", 30,"",true));
            arrColumn.Add(FormGlobals.Grid_NewColumn<DataGridViewTextBoxColumn>("Tên chức danh", "Grade_Name", 570));
            arrColumn.Add(FormGlobals.Grid_NewColumn<DataGridViewTextBoxColumn>("ID", "Grade_ID", 0));
            arrColumn.Add(FormGlobals.Grid_NewColumn<DataGridViewTextBoxColumn>("Đơn vị", "Grade_BranchCode",0));
            //
            m_GridHandle = FormGlobals.Grid_Init(dgvList, arrColumn, "", 1);
            m_GridHandle.Row_Validated += new DataGridHandle.Row_ValidatedEventHandler(m_GridHandle_Row_Validated);

        }

        void m_GridHandle_Row_Validated(DataGridViewRow row)
        {
            row.Cells["RN"].Value = row.Index + 1;
        }

        void LoadLanguage()
        {
            if(this.Tag!=null)
            this.Text = this.TabText = UpdateUI.GetString(this.Tag.ToString());
            UpdateUI.Load_ToolStrip(ref toolStrip1);
            UpdateUI.Load_ContextMenuStrip(ref mnuPopup);
        }

        //Load danh sách
        private void loadList()
        {
            //dtData = _wcfGD.Grade_GetList(out itemCount, Program.strBranchCode, currPage, pageSize).Tables[0];
            try
            {
                DataSet ds = Grade_Controller.Instance().Grade_GetList(out itemCount, Program.strBranchCode, currPage, pageSize);
                dtData = ds.Tables[0];
                //MessageBox.Show("So luong dong:" + dtData.Rows.Count + " so luong table " + ds.Tables.Count);
                dtData.Columns["Grade_BranchCode"].DefaultValue = Program.strBranchCode;
                Globals.DataTable_RowNumber(dtData);
                //dtData.DefaultView.Sort = "Grade_Name";
                //Globals.DataTable_SetAutoColumn(dtData, "STT");
                try
                {
                    Globals.DataTable_SetUnique(dtData, "Grade_BranchCode", "Grade_Name");
                }
                catch(Exception ex)
                {
                    FormGlobals.Message_Warning("Tồn tại ngạch/chức danh trùng tên");
                }
                
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
                //

                totalPage = itemCount % pageSize > 0 ? (itemCount / pageSize) + 1 : (itemCount / pageSize);
                if (totalPage == 0)
                    lblPageIndex.Text = "0" + "/" + totalPage.ToString();
                else
                    lblPageIndex.Text = (currPage + 1).ToString() + "/" + totalPage.ToString();

                dgvList.DataSource = dtData;
           
        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            if (!listRole.Create)
            {
                MessageBox.Show(UpdateUI.GetString("khongcoquyenthemmoi"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            Form f = new frmGradeInfo(0, listRole);
            if (f.ShowDialog(this) == DialogResult.OK)
            {
                loadList();
            }
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            if (dgvList.SelectedRows.Count > 0)
            {
                if (listRole.Update||listRole.View)
                {
                    _GDID = Convert.ToInt32(dgvList.CurrentRow.Cells["Grade_ID"].Value);
                    Form f = new frmGradeInfo(_GDID, listRole);
                    if (f.ShowDialog(this) == DialogResult.OK)
                    {
                        loadList();
                    }
                }
                else
                    MessageBox.Show(UpdateUI.GetString("khongcoquyenxem"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        private void btnDel_Click(object sender, EventArgs e)
        {
            if (dgvList.SelectedRows.Count > 0)
            {
                if (!listRole.Delete)
                {
                    MessageBox.Show(UpdateUI.GetString("khongcoquyenxoa"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                _GDID = Convert.ToInt32(dgvList.CurrentRow.Cells["Grade_ID"].Value);
                if (Grade_Controller.Instance().Grade_Using(_GDID) != 0)
                {
                    MessageBox.Show(UpdateUI.GetString("dasudung"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                if (MessageBox.Show( UpdateUI.GetString("bancochacchanxoa"),  UIParamater.TitleProject, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    if (Grade_Controller.Instance().Grade_Delete(_GDID))
                    {
                        Program.taskbarNotifier.Show( UIParamater.TitleProject,  UpdateUI.GetString("xoathanhcong"), UIParamater.TIME_MOVE_UP, UIParamater.TIME_SHOW, UIParamater.TIME_MOVE_DOWN);
                        loadList();
                    }
                    else
                    {
                        MessageBox.Show(UpdateUI.GetString("xoakhongthanhcong"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }

                }
            }

        }

        private void btnFisrt_Click(object sender, EventArgs e)
        {
            currPage = 0;
            loadList();
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            totalPage = itemCount % pageSize > 0 ? (itemCount / pageSize) + 1 : (itemCount / pageSize);
            if (totalPage > 0 && currPage != totalPage - 1)
                currPage++;
            loadList();
        }

        private void btnPrevious_Click(object sender, EventArgs e)
        {
            if (currPage != 0)
                currPage--;
            loadList();
        }

        private void btnLast_Click(object sender, EventArgs e)
        {
            totalPage = itemCount % pageSize > 0 ? (itemCount / pageSize) + 1 : (itemCount / pageSize);
            if (totalPage != 0)
                currPage = totalPage - 1;
            loadList();
        }

        private void dgvList_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if(e.RowIndex>=0)
            btnEdit_Click(null, null);
        }

        private void frmGradeList_KeyUp(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.Escape:
                    this.Dispose();
                    this.Close();
                    break;
                case Keys.F2:
                    btnNew_Click(null, null);
                    break;
                case Keys.F3:
                    btnEdit_Click(null, null);
                    break;
                case Keys.Delete:
                    btnDel_Click(null, null);
                    break;
            }
        }

        private void btnNewPopUp_Click(object sender, EventArgs e)
        {
            btnNew_Click(null, null);
        }

        private void btnEditPopUp_Click(object sender, EventArgs e)
        {
            btnEdit_Click(null, null);
        }

        private void btnDelPopUp_Click(object sender, EventArgs e)
        {
            btnDel_Click(null, null);
        }

        private void dgvList_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                if (dgvList.SelectedRows.Count > 0)
                {
                    mnuPopup.Enabled = true;
                    btnNewPopUp.Enabled = true;
                    btnEditPopUp.Enabled = true;
                    btnDelPopUp.Enabled = true;
                }
            }
        }

        private void frmGradeList_Activated(object sender, EventArgs e)
        {
            //LoadLanguage();
            if (dgvList.Columns.Count > 0)
            {
                dgvList.Columns["Grade_Name"].HeaderText = UpdateUI.GetString("ten");
            }

        }

        private void frmGradeList_FormClosing(object sender, FormClosingEventArgs e)
        {
            //_wcfGD.Close();
        }

        //Lưu dữ liệu
        private void btnSave_Click(object sender, EventArgs e)
        {
           // dtData.AcceptChanges();
            if(FormGlobals.Control_CheckError(dgvList))
            FormGlobals.App_ProgressShow(delegate() {
                Grade_Controller.Instance().Clear(Program.strBranchCode);
                Grade_Controller.Instance().InsertTable(dtData);
                
                //Load lại danh sách
                loadList();
            },"Lưu dữ liệu","Lưu thành công");
        }
    }
}
